import { ccs } from "../util/services";
import { TouchExtend } from "../util/TouchExd";
import { touchExd } from "../util/TouchExtend";
import { winConfig } from "../util/winConfig";

const {ccclass, property, disallowMultiple} = cc._decorator;
export interface KeyObject<T> {
    [index: string] : T;
}
@ccclass
export class WinBase extends cc.Component {
    _args: any ; //传入的参数
    nodes: KeyObject<cc.Node> = {};   
    _config: winConfig.configData = null;      

    private _isSwallow:boolean = false;
     _isClickClose:boolean = false;
    private _isMask:boolean = false;
     _isTouchBeginClose:boolean = false;
    private _isAddTouchEvent:boolean = false;      // 是否添加过触摸
    private _clickFunc:Function = null;             // 点击事件
    private __events = {};                          // 注册记录的事件
    _close() {
        // if(this.__isClean || this._shieldClose) return;
        // this.__isClean = true;
        // this._cleanup();
        // this._clearEvents();
        ccs.winMgr.cleanWin(this.getWinName());
        this.node.parent.destroy();
        // this._closeFuncCallback();
    }
    _initArgs(args?:any, winType?:number) {
        this.setInitArgs(args);
        ccs.gf.extendNodeName(this.nodes, this.node.parent);
        this._config = winConfig.getCfg(this.getWinName());

        // 添加屏蔽事件
        if(!this._config.notBlock) {
            if(!this.getComponent(cc.BlockInputEvents)) {
                this.addComponent(cc.BlockInputEvents);
            }
        }
        this.setDialogType.apply(this, winConfig.WIN_TYPE_CFG[winType || this._config.type]);
        // 关闭点击
        if(this.nodes.btn_close) {
            this.touchEndEventScale(this.nodes.btn_close, this._close, this);
        }
        if(this.nodes.map) {
            this.touchEndEventScale(this.nodes.map, this.showMap, this);
        }
    }
    // 设置窗口类型
    setDialogType(isSwallow:boolean, isClickClose:boolean, isMask:boolean, isTouchBeginClose:boolean = false) {
        this._isSwallow = isSwallow;
        this._isClickClose = isClickClose;
        this._isMask = isMask;
        this._isTouchBeginClose = isTouchBeginClose;

        this._addTouchEvent();
    }
    setInitArgs(args) {
        this._args = args || {};
    }
    getWinName() {
        return this.node.parent.name;
    }
     // 注册事件
    registerEvent(event:string, func:Function) {
        if(!this.__events[event]) {
            this.__events[event] = [];
        }
        // TODO：添加注册
        ccs.mainEmitter.on(event, func, this);
        this.__events[event].push(func);
    }
    showMap() {
        this._close();
        ccs.winMgr.showWin("mapWin");
    }
    // 移除某个事件
    removeEvent(event:string, func?:Function) {
        let events = this.__events[event];
        if(events) {
            if(func) {
                let idx = ccs.gf.findListIdx(events, func);
                if(idx > -1) {
                    // TODO: 清除事件
                    ccs.mainEmitter.off(event, func);
                    events.splice(idx, 1);
                }
            } else {
                for(let i = 0, len = events.length; i < len; i++) {
                    // TODO: 清除事件
                    ccs.mainEmitter.off(event, events[i]);
                }

                this.__events[event] = [];
            }
        }
    }
    // 点击函数
    protected touchEndEventScale(node:cc.Node, func:Function,target?:object,endFunc?: Function) {
        TouchExtend.touchEndScale(node, func, "", this);
    }
     // 添加窗口触摸事件
     private _addTouchEvent() {
        if(this._isAddTouchEvent) {
            return ;
        }

        if(this._isClickClose || this._isTouchBeginClose || this._clickFunc || this._isSwallow) {
            let closeFunc = () => {
                if(!cc.isValid(this.node.parent)) return;
                if(this.node.parent.active && this._isClickClose){
                    this._close();
                }
            };
            this.touchEndEventScale(this.node.parent, (event: cc.Event.EventTouch)=>{
                    ccs.gf.doCallback(this._clickFunc, this, event);
                    if(!this._isTouchBeginClose) {
                        closeFunc();
                    }
                },this, closeFunc);
            // touchCpt.setSwallowTouches(this._isSwallow);
            this._isAddTouchEvent = true;
        }
    }
}